﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TechnicalAnalysisEngine;
using System.Diagnostics;

namespace TechnicalAnalysisEngine.Tests
{
    [TestClass]
    public class TestEMA
    {
        List<double> testValues;
        List<double> expectedResults;

        [TestInitialize]
        public void Initialize()
        {
            testValues = new List<double>()
            {
                22.27, 22.19, 22.08, 22.17, 22.18, 22.13, 22.23, 22.43, 22.24,
                22.29, 22.15, 22.39, 22.38, 22.61, 22.36, 24.05, 23.75, 23.83, 23.95, 
                23.63, 23.82, 23.87, 23.65, 23.19, 23.10, 23.33, 22.68, 23.10, 22.40, 22.17
            };

            expectedResults = new List<double>()
            {
                22.22, 22.21, 22.24, 22.27, 22.33, 22.52, 22.80, 22.97, 23.13, 23.28, 23.34, 23.43, 23.51, 23.54,
                23.47, 23.40, 23.39, 23.26, 23.23, 23.08, 22.92
            };
        }

        [TestMethod]
        public void TestEMAMatureValues()
        {
            var result = AnalysisEngine.EMA(testValues, 10);

            Assert.IsTrue(expectedResults.Count == result.Values.Count, "Number of results doesn't match the expected value");

            for (int i = 0; i < result.Values.Count; i++)
            {
                if (!TestHelpers.EqualsWithTolerance(result.Values[i], expectedResults[i], 1))
                {
                    Assert.Fail("Values at position " + i.ToString() + " do not match");
                }
            }
        }

        [TestMethod]
        public void TestEMAPerformance()
        {
            int period = 10;

            var output = new List<double>();

            Random random = new Random();
            var randomlyGeneratedValues = new List<double>();

            for (int i = 0; i < 10000; i++)
            {
                randomlyGeneratedValues.Add(random.Next(85, 90));
            }

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();

            var result = AnalysisEngine.EMA(randomlyGeneratedValues, period);

            stopWatch.Stop();

            string trace = string.Format("EMA, {0} values, {1} μs", randomlyGeneratedValues.Count, stopWatch.ElapsedTicks / 10);
            Console.WriteLine(trace);
        }
    }
}
